package cn.sdormitory.controller.canteen;

import cn.hutool.extra.qrcode.QrCodeUtil;
import cn.sdormitory.canteen.service.CanteenBuyService;
import cn.sdormitory.canteen.vo.CanteenBuyVO;
import cn.sdormitory.common.api.CommonPage;
import cn.sdormitory.common.api.CommonResult;
import cn.sdormitory.school.entity.SchoolDuty;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.Authorization;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.io.OutputStream;
import java.util.Map;

/**
 * 食堂原料采购
 */
@RestController
@Api(tags = "canteen-buy=> 食堂原料采购")
@RequestMapping("/canteen/buy")
public class CanteenBuyController {

    @Autowired
    private CanteenBuyService canteenBuyService;

    @ApiOperation("getBuyPageList => 分页查询采购信息")
    @PostMapping(value = "/getBuyPageList")
    public CommonResult<CommonPage<CanteenBuyVO>> getBuyPageList(@RequestBody Map<String, Object> params){
        CommonPage<CanteenBuyVO> pagelist = canteenBuyService.getPage(params);
        return CommonResult.success(pagelist);
    }

    @ApiOperation("createOrUpdateCanteenBuyInfo => 新增or修改查询采购信息")
    @PostMapping(value = "/createOrUpdateCanteenBuyInfo")
    public CommonResult<Integer> createOrUpdateCanteenBuyInfo(@RequestBody CanteenBuyVO vo){
        int num = canteenBuyService.createOrUpdateCanteenBuyInfo(vo);
        if(num>0){
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    @ApiOperation("deleteCanteenBuyInfo => 新增or修改查询采购信息")
    @PostMapping(value = "/deleteCanteenBuyInfo")
    public CommonResult<Integer> deleteCanteenBuyInfo(@RequestBody CanteenBuyVO vo){
        int num = canteenBuyService.deleteCanteenBuyById(vo.getId());
        if(num>0){
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    @ApiOperation("goodsPutStock => 入库修改")
    @PostMapping(value = "/goodsPutStock")
    public CommonResult<Integer> goodsPutStock(@RequestBody CanteenBuyVO vo){
        int num = canteenBuyService.goodsPutStock(vo);
        if(num>0){
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    @GetMapping("/getQR")
    public void getGoodsOrderQr(ServletRequest request, ServletResponse response) throws Exception{
        String url = request.getParameter("url");
        OutputStream out = response.getOutputStream();
        QrCodeUtil.generate(url,200,200,null,out);
    }


}
